package com.ts.view;


import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;

import com.easemob.EMCallBack;
import com.easemob.chat.EMChatManager;
import com.rio.core.U;
import com.rio.layout.LayoutManager;
import com.rio.layout.PopupManager;
import com.rio.layout.WindowBuilder.OnDismissListener;
import com.rio.layout.view.SimpleWindow;
import com.ts.host.APP;
import com.ts.host.Event;
import com.ts.host.R;
import com.ts.host.T;
import com.ts.layout.DebugLayout;
import com.ts.manager.HttpMananger;
import com.ts.manager.MachineManager;
import com.ts.manager.WaterManager;

import de.greenrobot.event.EventBus;

public class QuitDailog extends SimpleWindow implements OnClickListener,OnDismissListener{
	
	private static QuitDailog mDailog;
	private int mAction;
	
	public static QuitDailog getInstance(){
		if(U.isNull(mDailog)){
			mDailog = new QuitDailog();
		}
		return mDailog;
	}

	public void show(){
		PopupManager.getInstance().setOnWindowListener(this);
		PopupManager.getInstance().show(this);
	}
	
	public void destory(){
		mDailog = null;
	}


	@Override
	public View onAttach(LayoutInflater layout, Object... params) {
		View view = APP.inflate(R.layout.dialog_quit);
		T.setOnClickListener(view, this, R.id.btn_0,R.id.btn_1,R.id.btn_2,R.id.btn_3);
		return view;
	}



	@Override
	public void onClick(View view) {
		mAction = view.getId();
		PopupManager.getInstance().hideWindow();
	}

	@Override
	public void onDismiss(String windowname, Object... params) {
		switch (mAction) {
		case R.id.btn_0:
			if (!MachineManager.getInstance().isConnected()) {
				HttpMananger.regMachine();
			} else {
				HttpMananger.getMachineAddress();
			}
			break;
		case R.id.btn_1:
			LayoutManager.getInstance().add(new DebugLayout());
			break;
		case R.id.btn_2:
			mDailog = null;
			EMChatManager.getInstance().logout(new EMCallBack() {
				
				@Override
				public void onSuccess() {
					// TODO Auto-generated method stub
					LayoutManager.getInstance().finish();
				}
				
				@Override
				public void onProgress(int arg0, String arg1) {
					// TODO Auto-generated method stub
					
				}
				
				@Override
				public void onError(int arg0, String arg1) {
					// TODO Auto-generated method stub
					
				}
			});
			
			break;
		default:
			break;
		}

		
	}

}
